package com.project.common.tag;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.project.service.tags.WidgetService;


/**
 * @author kale
 * Accessing database to generate select input widget according to the id which passed in this widget.
 */
@Component
public class WriteClassTag extends TagSupport {

	private static final long serialVersionUID = -7762361372178555026L;

	private static Logger logger = LoggerFactory.getLogger(WriteClassTag.class);
	
	private String type = null;
	private String value = null;
	
	private List<Map<String, String>> list;

	private WidgetService widgetService;

	public int doEndTag() throws JspException{
		
		StringBuffer results = new StringBuffer("");
		if ((this.value != null) && (!(this.value.trim().equals("")))) {
			String condspVal = getSysConDspval(this.value.trim());
			if (condspVal != null){
				if(this.value.equals("0")){
					results.append("<span class=\"label label-sm-12 label-warning\">");
				}else if(this.value.equals("1")){
					results.append("<span class=\"label label-sm-12 label-success\">");
				}else if(this.value.equals("2")){
					results.append("<span class=\"label label-sm-12 label-inverse\">");
				}else if(this.value.equals("3")){
					results.append("<span class=\"label label-sm-12 label-grey\">");
				}else{
					results.append("<span class=\"label label-sm-12 label-important\">");
				}
				results.append(condspVal);
				results.append("</span>");
			}
		}
		
		try {

			this.pageContext.getOut().print(results.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
		return 0;
             
	}
	
	protected String getSysConDspval(String conVal) {
		if (this.type == null) {
			return null;
		}
		WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(pageContext.getServletContext());   
		widgetService = (WidgetService)ctx.getBean("defaultWidgetService");
		list = this.getList();
		if ((list != null) && (!(list.isEmpty()))) {
			for(int i=0;i<list.size();i++){
	   			Map<String, String> map = (Map<String, String>) list.get(i);
	   			if(map.keySet().iterator().hasNext()){
	   				String key = map.keySet().iterator().next();
	   				if (map.get(key).equals(conVal)) {
						return key;
					}
	   			}
	   		}
		}
		return "";
	}
	
	protected void prepareAttribute(StringBuffer handlers, String name,
			Object value) {
		if (value != null) {
			handlers.append(" ");
			handlers.append(name);
			handlers.append("=\"");
			handlers.append(value);
			handlers.append("\"");
		}
	}
	

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	/**
	 * @return the list
	 */
	public List<Map<String, String>> getList() {
		ServletContext content=pageContext.getSession().getServletContext();
		List<Map<String, String>>  dicMap=(List<Map<String, String>>) content.getAttribute(type);
		if(dicMap==null){
			 List<Map<String, String>> newDicMap=widgetService.getSelectOptions(type);
			 content.setAttribute(type, newDicMap);
		}
		dicMap=(List<Map<String, String>>) content.getAttribute(type);
		return dicMap;
	}
}
